<template>
  <div class="contain">
    <div class="btn-box">
      <div @click="start" class="battle-btn btn">初始化</div>
      <div @click="startBattle" class="battle-btn btn">一场战斗</div>
      <div @click="battlePool" class="battle-btn btn">一键战斗</div>
      <div class="data-box">
        <div class="data-item">
          攻方胜场数：{{ attCount }} 胜率：{{
            attCount / (attCount + defCount + drawCount)
          }}
        </div>
        <div class="data-item">
          破城胜场数：{{ cityCount }} 胜率：{{
            cityCount / (attCount + defCount + drawCount)
          }}
        </div>
        <div class="data-item">胜利破城率：{{ cityCount / attCount }}</div>
        <div class="data-item">
          防守胜场数：{{ defCount }} 胜率：{{
            defCount / (attCount + defCount + drawCount)
          }}
        </div>
        <div class="data-item">
          平局场数：{{ drawCount }}平局率：{{
            drawCount / (attCount + defCount + drawCount)
          }}
        </div>
        <div
          class="data-item-item"
          v-for="item in baseBattleList"
          :key="item.typeCode"
        >
          <div>
            {{ item.cardName }} ：克制{{ item.suppressCount || 0 }} --平局{{
              item.drawSuppressCount || 0
            }}--被克制{{ item.bySuppressCount || 0 }}
          </div>
          <div>
            平均点数：{{
              (item.suppressCount * 3 +
                item.drawSuppressCount * 3 +
                item.bySuppressCount * 0) /
              (attCount + defCount + drawCount)
            }}
          </div>
        </div>
      </div>
    </div>
    <div class="desc-box">
      <div v-for="(item, index) in logList" :key="index">
        {{ index < 9 ? "0" + (index + 1) : index + 1 }}、 {{ item }}
      </div>
    </div>
  </div>
</template>
<script>
</script>
<script>
export default {
  data() {
    return {
      logList: [],
      personList1: [], //人物卡
      personList2: [], //人物卡
      spellList1: [], //计谋卡
      spellList2: [], //计谋卡
      battleList1: [], //指挥卡/战斗卡
      battleList2: [], //指挥卡/战斗卡
      personListData: [
        {
          id: "001001",
          cardName: "张飞",
          pow: 10,
          int: 2,
          desc: "武将",
          bubing: 3,
          qibing: 0,
          gongbing: 0,
          gongcheng: 0,
        },
        {
          id: "001002",
          cardName: "吕布",
          pow: 12,
          int: 2,
          desc: "武将",
          qibing: 3,
          gongbing: 3,
          bubing: 0,
          gongcheng: 0,
        },
        {
          id: "001003",
          cardName: "太史慈",
          pow: 10,
          int: 4,
          desc: "武将",
          qibing: 0,
          gongbing: 3,
          bubing: 1,
          gongcheng: 0,
        },
        {
          id: "001004",
          cardName: "典韦",
          pow: 10,
          int: 2,
          desc: "武将",
          qibing: 1,
          gongbing: 0,
          bubing: 3,
          gongcheng: 0,
        },
        {
          id: "001005",
          cardName: "关羽",
          pow: 10,
          int: 6,
          desc: "武将",
          qibing: 3,
          gongbing: 2,
          bubing: 0,
          gongcheng: 0,
        },
        {
          id: "001006",
          cardName: "曹仁",
          pow: 8,
          int: 6,
          desc: "武将",
          qibing: 2,
          gongbing: 1,
          bubing: 2,
          gongcheng: 0,
        },
        {
          id: "002001",
          cardName: "徐庶",
          pow: 6,
          int: 10,
          desc: "谋士",
          qibing: 1,
          gongbing: 0,
          bubing: 0,
          gongcheng: 2,
        },
        {
          id: "002002",
          cardName: "刘晔",
          pow: 4,
          int: 9,
          desc: "谋士",
          qibing: 1,
          gongbing: 1,
          bubing: 1,
          gongcheng: 3,
        },
        {
          id: "002003",
          cardName: "诸葛亮",
          pow: 4,
          int: 12,
          desc: "谋士",
          qibing: 0,
          gongbing: 3,
          bubing: 1,
          gongcheng: 3,
        },
        {
          id: "002004",
          cardName: "荀彧",
          pow: 2,
          int: 10,
          desc: "武将",
          qibing: 0,
          gongbing: 2,
          bubing: 0,
          gongcheng: 2,
        },
        {
          id: "002005",
          cardName: "周瑜",
          pow: 6,
          int: 10,
          desc: "武将",
          qibing: 1,
          gongbing: 2,
          bubing: 1,
          gongcheng: 3,
        },
      ], //人物卡数据
      spellListData: [], //计谋卡数据
      battleListData: [], //指挥卡/战斗卡数据
      baseBattleList: [
        {
          cardName: "骑兵冲锋",
          point: 3,
          type: "qibing",
          typeCode: "q1",
          suppress: ["g1"],
          suppressCount: 0,
          bySuppressCount: 0,
          drawSuppressCount: 0,
        },
        {
          cardName: "骑兵迂回",
          point: 3,
          type: "qibing",
          typeCode: "q2",
          suppress: ["q1", "b3"],
          suppressCount: 0,
          bySuppressCount: 0,
          drawSuppressCount: 0,
        },
        {
          cardName: "步兵盾守",
          point: 3,
          type: "bubing",
          typeCode: "b1",
          suppress: [],
          suppressCount: 0,
          bySuppressCount: 0,
          drawSuppressCount: 0,
        },
        {
          cardName: "步兵枪阵",
          point: 3,
          type: "bubing",
          typeCode: "b2",
          suppress: ["q1"],
          suppressCount: 0,
          bySuppressCount: 0,
          drawSuppressCount: 0,
        },
        {
          cardName: "步兵伏击",
          point: 3,
          type: "bubing",
          typeCode: "b3",
          suppress: ["g1", "b4"],
          suppressCount: 0,
          bySuppressCount: 0,
          drawSuppressCount: 0,
        },
        {
          cardName: "弓兵远射",
          point: 3,
          type: "gongbing",
          typeCode: "g1",
          suppress: ["q2", "b2"],
          suppressCount: 0,
          bySuppressCount: 0,
          drawSuppressCount: 0,
        },
        {
          cardName: "骑兵践踏",
          point: 3,
          type: "qibing",
          typeCode: "q3",
          suppress: ["b1"],
          suppressCount: 0,
          bySuppressCount: 0,
          drawSuppressCount: 0,
        },
        {
          cardName: "步兵肉搏",
          point: 3,
          type: "bubing",
          typeCode: "b4",
          suppress: ["g2", "q3"],
          suppressCount: 0,
          bySuppressCount: 0,
          drawSuppressCount: 0,
        },
        {
          cardName: "弓兵穿射",
          point: 3,
          type: "gongbing",
          typeCode: "g2",
          suppress: ["b1"],
          suppressCount: 0,
          bySuppressCount: 0,
          drawSuppressCount: 0,
        },
      ],
      attCount: 0, //攻方胜场
      cityCount: 0, //破城胜场
      defCount: 0, //防守胜场
      drawCount: 0, //平局场
      //战局数据
      hp1: 6,
      hp2: 6,
      city: 1,
      hp2_: 6,
      ended: false,
      loading: false,
      sepllPoint1: 0,
      sepllPoint2: 0,
    };
  },
  computed: {},
  methods: {
    //打乱数组
    shuffleArray(array) {
      for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]];
      }
      return array;
    },
    //抽一张
    randomCards(arr) {
      if (arr.length === 0) return { cardName: "稻草人" }; // 如果数组为空，直接返回
      const index = Math.floor(Math.random() * arr.length); // 生成一个随机索引
      return arr.splice(index, 1)[0];
    },
    //抽一张
    randomCardsLike(arr, card) {
      const newArr = JSON.parse(JSON.stringify(arr));
      let resVal = 0;
      let resIndex = 0;

      for (var i = 0; i < newArr.length; i++) {
        if (card[newArr[i].type] > resVal && Math.random() > 0.5) {
          resIndex = i;
        }
      }

      return arr.splice(resIndex, 1)[0];
    },
    saveLog(text) {
      this.logList.push(text);
    },
    start() {
      this.logList = [];
      this.hp1 = 6;
      this.hp2 = 6;
      this.city = 0;
      this.hp2_ = 6;
      this.ended = false;
      this.sepllPoint1 = 0;
      this.sepllPoint2 = 0;

      //初始化配置武将卡
      //   this.personList1 = JSON.parse(
      //     JSON.stringify(this.shuffleArray(this.personListData))
      //   );
      //   this.personList2 = JSON.parse(
      //     JSON.stringify(this.shuffleArray(this.personListData))
      //   );
      this.personList1 = [
        {
          id: "001001",
          cardName: "关羽",
          pow: 10,
          int: 2,
          desc: "武将",
          bubing: 0,
          qibing: 0,
          gongbing: 0,
          gongcheng: 0,
        },
        {
          id: "002001",
          cardName: "徐庶",
          pow: 6,
          int: 10,
          desc: "谋士",
          qibing: 1,
          gongbing: 0,
          bubing: 0,
          gongcheng: 2,
        },
      ];
      this.personList2 = [
        {
          id: "001002",
          cardName: "华雄",
          pow: 10,
          int: 2,
          desc: "武将",
          qibing: 0,
          gongbing: 0,
          bubing: 0,
          gongcheng: 0,
        },
        {
          id: "002004",
          cardName: "诸葛亮",
          pow: 2,
          int: 12,
          desc: "武将",
          qibing: 0,
          gongbing: 2,
          bubing: 0,
          gongcheng: 2,
        },
      ];

      //初始化配置战斗卡
      this.battleList1 = JSON.parse(
        JSON.stringify(this.shuffleArray(this.baseBattleList))
      );
      this.battleList2 = JSON.parse(
        JSON.stringify(this.shuffleArray(this.baseBattleList))
      );
    },
    startBattle() {
      this.start();
      this.saveLog(
        `攻击士气${this.hp1}-vs-防御士气${this.hp2}+城防${this.city}`
      );
      this.hp2_ = this.hp2 + this.city;
      //选择攻击方2张武将，3张谋略，3张指挥
      let card1 = this.randomCards(this.personList1);
      let card2 = this.randomCards(this.personList1);
      let card0 = {};
      if (
        card1.pow < card2.pow ||
        (card1.pow == card2.pow && card1.int > card2.int)
      ) {
        card0 = card2;
        card2 = card1;
        card1 = card0;
      }
      this.saveLog(`攻击武将-先锋${card1.cardName}-${card1.pow}`);
      this.saveLog(`攻击武将-军师${card2.cardName}-${card2.int}`);
      let battle1 = this.randomCardsLike(this.battleList1, card1);
      let battle2 = this.randomCardsLike(this.battleList1, card1);
      let battle3 = this.randomCardsLike(this.battleList1, card1);
      this.saveLog(
        `攻击战斗卡：${battle1.cardName}、${battle2.cardName}、${battle3.cardName}`
      );
      //选择防御方2张武将，3张谋略，3张指挥
      let card3 = this.randomCards(this.personList2);
      let card4 = this.randomCards(this.personList2);
      if (
        card3.pow < card4.pow ||
        (card3.pow == card4.pow && card3.int > card4.int)
      ) {
        card0 = card4;
        card4 = card3;
        card3 = card0;
      }
      this.saveLog(`防御武将-先锋${card3.cardName}-${card3.pow}`);
      this.saveLog(`防御武将-军师${card4.cardName}-${card4.int}`);
      let battle4 = this.randomCardsLike(this.battleList2, card3);
      let battle5 = this.randomCardsLike(this.battleList2, card3);
      let battle6 = this.randomCardsLike(this.battleList2, card3);
      this.saveLog(
        `防守战斗卡：${battle4.cardName}、${battle5.cardName}、${battle6.cardName}`
      );
      this.sepllPoint1 = card2.int;
      this.sepllPoint2 = card4.int;

      this.saveLog(
        `第一阵：【${battle1.cardName}】-对-【${battle4.cardName}】`
      );
      this.handleBattle(card1, card3, battle1, battle4);
      if (this.ended) return;

      this.saveLog(
        `第二阵：【${battle2.cardName}】-对-【${battle5.cardName}】`
      );
      this.handleBattle(card1, card3, battle2, battle5);
      if (this.ended) return;

      this.saveLog(
        `第三阵：【${battle3.cardName}】-对-【${battle6.cardName}】`
      );
      this.handleBattle(card1, card3, battle3, battle6);
      if (this.ended) return;
      if (this.hp1 > this.hp2_) {
        this.saveLog(
          `终局：攻击胜利。攻击士气${this.hp1}-vs-防御士气${this.hp2_}`
        );
        this.attCount++;
      }
      if (this.hp1 < this.hp2_) {
        this.saveLog(
          `终局：防御胜利。攻击士气${this.hp1}-vs-防御士气${this.hp2_}`
        );
        this.defCount++;
      }
      if (this.hp1 == this.hp2_) {
        this.saveLog(`终局：打平。攻击士气${this.hp1}-vs-防御士气${this.hp2_}`);
        this.drawCount++;
      }
    },
    handleBattle(attCard, defCard, attBattle, defBattle) {
      if (this.ended) return;
      //武将兵种专精结算
      let attTypeBuff = attCard[attBattle.type];
      let defTypeBuff = defCard[defBattle.type];
      this.saveLog(`${attCard.cardName}兵种专精：${attTypeBuff}`);
      this.saveLog(`${defCard.cardName}兵种专精：${defTypeBuff}`);
      //插入谋略卡结算todo
      let spellBuff1 = Math.floor(Math.random() * this.sepllPoint1);
      let spellBuff2 = Math.floor(Math.random() * this.sepllPoint2);
      this.saveLog(`攻击谋略${spellBuff1}-vs-防御谋略${spellBuff2}`);
      this.sepllPoint1 -= spellBuff1;
      this.sepllPoint2 -= spellBuff2;
      if (attBattle.suppress.includes(defBattle.typeCode)) {
        defBattle.point = 0;
        defTypeBuff = 0;
        spellBuff2 = 0;
        this.saveLog(`【${attBattle.cardName}】克制【${defBattle.cardName}】`);
        this.baseBattleList.forEach((ele) => {
          if (ele.typeCode == attBattle.typeCode) {
            ele.suppressCount++;
          }
          if (ele.typeCode == defBattle.typeCode) {
            ele.bySuppressCount++;
          }
        });
      } else if (defBattle.suppress.includes(attBattle.typeCode)) {
        attBattle.point = 0;
        attTypeBuff = 0;
        spellBuff1 = 0;
        this.saveLog(
          `【${defBattle.cardName}】被克制【${attBattle.cardName}】`
        );
        this.baseBattleList.forEach((ele) => {
          if (ele.typeCode == attBattle.typeCode) {
            ele.bySuppressCount++;
          }
          if (ele.typeCode == defBattle.typeCode) {
            ele.suppressCount++;
          }
        });
      } else {
        this.baseBattleList.forEach((ele) => {
          if (ele.typeCode == attBattle.typeCode) {
            ele.drawSuppressCount++;
          }
          if (ele.typeCode == defBattle.typeCode) {
            ele.drawSuppressCount++;
          }
        });
      }
      //武将战法结算todo

      let res =
        attCard.pow +
        attBattle.point +
        attTypeBuff +
        spellBuff1 -
        (defCard.pow + defBattle.point + defTypeBuff + spellBuff2);
      this.saveLog(
        `${res} 攻击 ${
          attCard.pow + attBattle.point + attTypeBuff + spellBuff1
        }-vs-防御  ${defCard.pow + defBattle.point + defTypeBuff + spellBuff2}`
      );
      this.hp1 += res;
      this.hp2_ -= res;

      if (this.hp1 <= 0) {
        this.saveLog(
          `终局：防守胜利。 攻击士气${this.hp1}-vs-防御士气${this.hp2_}`
        );
        this.defCount++;
        this.ended = true;
        return;
      }
      if (this.hp2_ <= 0) {
        this.saveLog(
          `终局：攻击胜利。破城！ 攻击士气${this.hp1}-vs-防御士气${this.hp2_}`
        );

        this.attCount++;
        this.cityCount++;
        this.ended = true;
        return;
      }
      if (res > 0) {
        this.saveLog(
          `对阵结果：攻击胜利。 攻击士气${this.hp1}-vs-防御士气${this.hp2_}`
        );
      } else if (res < 0) {
        this.saveLog(
          `对阵结果：防御胜利。 攻击士气${this.hp1}-vs-防御士气${this.hp2_}`
        );
      } else {
        this.saveLog(
          `对阵结果：平局。 攻击士气${this.hp1}-vs-防御士气${this.hp2_}`
        );
      }
    },
    battlePool() {
      this.loading = true;
      for (var i = 0; i < 5000; i++) {
        this.startBattle();
      }
      this.loading = false;
    },
  },
  mounted() {
    this.start();
  },
};
</script>
<style scoped >
.contain {
  width: 100%;
  height: 100%;
  display: flex;
}
.btn-box {
  width: 400px;
  height: 600px;
  padding: 60px;
}
.btn {
  width: 140px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  border-radius: 6px;
  color: #fff;
  background-color: #ff7300;
  cursor: pointer;
  margin: 14px;
}

.desc-box {
  flex: 1;
  padding: 60px;
  text-align: start;
}
.data-box {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  padding: 16px;
}
.data-item {
  height: 40px;
  line-height: 40px;
}
.data-item-item {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  padding: 16px;
}
</style>